package com.beiing.tianshuai.tianshuai.dongtai;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.activity.MainActivity;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.dongtai.presenter.DynamicForwardPresenter;
import com.beiing.tianshuai.tianshuai.dongtai.view.DynamicForwardViewImpl;
import com.beiing.tianshuai.tianshuai.entity.DynamicBean;
import com.beiing.tianshuai.tianshuai.fragment.EmotionCompleteFragment;
import com.beiing.tianshuai.tianshuai.util.GlobalOnItemClickManagerUtils;
import com.beiing.tianshuai.tianshuai.util.SPUtils;
import com.beiing.tianshuai.tianshuai.util.SpanStringUtils;
import com.bumptech.glide.Glide;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import butterknife.BindView;

/**
 * Created by LZQ on 2017/8/4.
 * 添加动态
 */

public class DynamicForwardActivity extends BaseActivity implements DynamicForwardViewImpl, View.OnClickListener {

    @BindView(R.id.toolbar_iv_back)
    ImageView toolbarIvBack;
    @BindView(R.id.toolbar_rv_back)
    RelativeLayout toolbarRvBack;
    @BindView(R.id.toolbar_center_title)
    TextView toolbarCenterTitle;
    @BindView(R.id.toolbar_tv_title)
    TextView toolbarTvTitle;
    @BindView(R.id.toolbar_iv_publish)
    ImageView toolbarIvPublish;
    @BindView(R.id.toolbar_btn_publish)
    TextView toolbarBtnPublish;
    @BindView(R.id.dynamic_forward_content)
    EditText dynamicForwardContent;
    @BindView(R.id.text_num)
    TextView textNum;
    @BindView(R.id.dynamic_forward_img)
    ImageView dynamicForwardImg;
    @BindView(R.id.dynamic_forward_from_user)
    TextView dynamicForwardFromUser;
    @BindView(R.id.dynamic_forward_from_content)
    TextView dynamicForwardFromContent;
    @BindView(R.id.ll_emoji_input)
    LinearLayout llEmojiInput;
    @BindView(R.id.iv_face)
    ImageView ivFace;
    @BindView(R.id.iv_keyboard)
    ImageView ivKeyBoard;
    @BindView(R.id.fl_emotionview_main)
    FrameLayout mEmotionLayout;

    private DynamicBean.DataBean.DynamicsBean dynamics;
    private DynamicForwardPresenter mPresenter;
    private String from_uid;
    private String to_uid;
    private InputMethodManager imm;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_dynamic_forward;
    }

    @Override
    protected void init() {
        initToolbar();
        initData();
        initPresenter();
        initEmotionMainFragment();
        initListener();
        imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        llEmojiInput.setVisibility(View.VISIBLE);
    }

    private void initEmotionMainFragment() {
        GlobalOnItemClickManagerUtils globalOnItemClickManager = GlobalOnItemClickManagerUtils.getInstance(mContext);
        globalOnItemClickManager.attachToEditText(dynamicForwardContent);
        EmotionCompleteFragment emotionCompleteFragment = new EmotionCompleteFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fl_emotionview_main, emotionCompleteFragment);
        transaction.commit();
    }

    private void initPresenter() {
        mPresenter = new DynamicForwardPresenter(this);
    }

    private void initToolbar() {
        toolbarTvTitle.setVisibility(View.GONE);
        toolbarCenterTitle.setVisibility(View.VISIBLE);
        toolbarCenterTitle.setText("转发");
        toolbarIvBack.setVisibility(View.VISIBLE);
        toolbarBtnPublish.setVisibility(View.VISIBLE);
        toolbarRvBack.setOnClickListener(this);
        toolbarBtnPublish.setOnClickListener(this);
    }

    private void initData() {
        Intent intent = this.getIntent();
        dynamics = (DynamicBean.DataBean.DynamicsBean) intent.getSerializableExtra("dynamic");
        if (dynamics == null) {
            finish();
        }
        dynamicForwardFromUser.setText(TextUtils.isEmpty(dynamics.getName()) ? "" : dynamics.getName());
        try {
            String contentDecode = URLDecoder.decode(dynamics.getContent(), "UTF-8");
            dynamicForwardFromContent.setText(SpanStringUtils.getEmotionContent(mContext, dynamicForwardFromContent, contentDecode));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        if (dynamics.getPic() != null && dynamics.getPic().size() > 0) {
            Glide.with(mContext.getApplicationContext())
                    .load(HttpRequestConstant.DYNAMIC_PIC_HEAD + dynamics.getPic().get(0).getPicname())
                    .into(dynamicForwardImg);
        }
        from_uid = SPUtils.getString(mContext, "uid", null);
        to_uid = dynamics.getUid();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_rv_back:
                finish();
                break;
            case R.id.toolbar_btn_publish:
                String content = dynamicForwardContent.getText().toString().trim();
                try {
                    String contentUTF8 = URLEncoder.encode(content, "UTF-8");
                    mPresenter.pushMyForward(from_uid, to_uid, dynamics.getId(), contentUTF8);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                break;
            default:
                break;
        }
    }

    private void initListener() {
        dynamicForwardContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mEmotionLayout.setVisibility(View.GONE);
                ivFace.setVisibility(View.VISIBLE);
                ivKeyBoard.setVisibility(View.GONE);
            }
        });

        ivFace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ivFace.setVisibility(View.GONE);
                ivKeyBoard.setVisibility(View.VISIBLE);
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
                mEmotionLayout.setVisibility(View.VISIBLE);
            }
        });

        ivKeyBoard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mEmotionLayout.setVisibility(View.GONE);
                ivKeyBoard.setVisibility(View.GONE);
                ivFace.setVisibility(View.VISIBLE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                dynamicForwardContent.setFocusable(true);
                dynamicForwardContent.setFocusableInTouchMode(true);
                dynamicForwardContent.requestFocus();
            }
        });
    }

    @Override
    public void showForwardDynamic() {
        MainActivity.isPublished = true;
        finish();
    }

    @Override
    public void onBackPressed() {
        if (mEmotionLayout.getVisibility() == View.VISIBLE) {
            mEmotionLayout.setVisibility(View.GONE);
        } else {
            super.onBackPressed();
        }
    }
}
